<template>
  <el-container>
    <el-aside :width="isCollapse?'65px' : '200px'">
      <el-menu
        background-color="#353B4E"
        text-color="#ccc"
        active-text-color="#F5DE19"
        :collapse="isCollapse"
        :collapse-transition="false"
        router
        :default-active="$route.path"
      >
        <el-menu-item index="0" id="logo_s" :style="{width:isCollapse?'65px' : '200px'}">
          <img src="./logo_admin.png" />
        </el-menu-item>

        <el-menu-item index="/welcome" :style="{width:isCollapse?'65px' : '200px'}">
          <i class="el-icon-location"></i>
          <span slot="title">首页</span>
        </el-menu-item>

        <el-submenu index="2" :style="{width:isCollapse?'65px' : '200px'}">
          <template slot="title">
            <i class="el-icon-menu"></i>
            <span>内容管理</span>
          </template>
          <el-menu-item index="/articleadd">发布文章</el-menu-item>
          <el-menu-item index="/article">文章列表</el-menu-item>
          <el-menu-item index="2-3">评论列表</el-menu-item>
          <el-menu-item index="/material">素材管理</el-menu-item>
        </el-submenu>

        <el-menu-item index="/fans" :style="{width:isCollapse?'65px' : '200px'}">
          <i class="el-icon-s-custom"></i>
          <span slot="title">粉丝管理</span>
        </el-menu-item>

        <el-menu-item index="/account" :style="{width:isCollapse?'65px' : '200px'}">
          <i class="el-icon-s-order"></i>
          <span slot="title">账户信息</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header>
        <div id="lt">
          <i
            :class="isCollapse? 'el-icon-s-unfold':'el-icon-s-fold'"
            @click="isCollapse=!isCollapse"
          ></i>
          <span style="margin-left:10px;">小贱头条自媒体平台</span>
        </div>

        <div id="rt">
          <el-input type="text" placeholder="请输入搜索的文章内容" style="width:240px;" v-model="search"></el-input>
          <span style="margin: 0 20px;">消息</span>
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              <img
                :src="photo"
                alt
                width="40px"
                height="40px"
                style="border-radius: 50%; margin-right:10px"
              />
              {{name}}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push('/account')">个人信息</el-dropdown-item>
              <el-dropdown-item>git地址</el-dropdown-item>
              <el-dropdown-item @click.native="logout()">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 导入bus
import bus from '@/utils/bus.js'

export default {
  name: 'HomeCom',
  data () {
    return {
      tmpname: '', // 临时接收更新的名字信息
      tmpphoto: '', // 临时接收更新的头像信息
      isCollapse: false, // 菜单是否折叠
      search: ''
    }
  },
  created () {
    // bus声明接收数据的事件方法
    // 更新账户的名字
    bus.$on('upAccountName', nm => {
      // 1:对名字进行更新，对tmpname进行赋值
      this.tmpname = nm
      // 2:对sessionStorage数据进行更细
      let userinfo = JSON.parse(window.sessionStorage.getItem('userinfo'))
      userinfo.name = nm// 名字永久更新
      window.sessionStorage.setItem('userinfo', JSON.stringify(userinfo))
    })
    // 更新账户的头像
    bus.$on('upAccountPhoto', ph => {
      // 1:对头像进行更新，对tmpphoto进行赋值
      this.tmpphoto = ph
      // 2:对sessionStorage数据进行更细
      let userinfo = JSON.parse(window.sessionStorage.getItem('userinfo'))
      userinfo.photo = ph// 头像永久更新
      window.sessionStorage.setItem('userinfo', JSON.stringify(userinfo))
    })
  },
  methods: {
    // 退出系统
    logout () {
      this.$confirm('确认要退出系统么？', '退出', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 清空sessionStorage，导航到登录页面
          window.sessionStorage.clear()
          this.$router.push('/login')
        })
        .catch(() => {})
    }
  },
  computed: {
    name () {
      // 名称
      return this.tmpname || JSON.parse(window.sessionStorage.getItem('userinfo')).name
    },
    photo () {
      // 头像
      return this.tmpphoto || JSON.parse(window.sessionStorage.getItem('userinfo')).photo
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-aside {
    background-color: #323745;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    padding: 0 10px;
    min-width: 800px;
    background-color: white;

    #lt {
      height: 100%;
      width: 40%;
      font-size: 20px;
      display: flex;
      align-items: center;
    }
    #rt {
      height: 100%;
      width: 50%;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      .el-dropdown-link {
        display: flex;
        align-items: center;
        height: 100%;
      }
    }
  }
  .el-main {
    background-color: #f2f3f5;
  }
  #logo_s {
    text-align: center;
    height: 60px;
    img {
      width: 80%;
    }
    cursor: default;
    &:hover{
      background-color: rgb(53, 59, 78) !important;
    }
  }
}
</style>
